home *** CD-ROM | disk | FTP | other *** search
- #include <time.h>
- #include <math.h>
- #include <fstream.h>
- #include <dinput.h>
-
- #include "tankwars.h"
-
- GLvoid BuildLists(void);
- int DI_Init();
-
- extern GLuint texture[TOTAL_TEXTURES];
-
- extern int heightmap[31][21];
- extern unsigned char lightmap[30][20];
- extern int grid[30][20];
-
- float explosionmap[20][20];
-
- extern float cameraangle;
-
- extern c_player player[2];
-
- extern c_explosion explosion[50];
- extern int nextexplosionslot;
-
- extern c_missle missle[20];
- extern int nextmissleslot;
-
- extern c_smoke smoke[50];
- extern int nextsmokeslot;
-
- extern c_grenade grenade[20];
- extern int nextgrenadeslot;
-
- extern c_particle particle[500];
- extern int nextparticleslot;
-
- extern c_debris debris[100];
- extern int nextdebrisslot;
-
- extern unsigned long wall;
-
- extern c_rain rain[500];
-
- extern int poweruptime;
-
- extern int winner;
-
- extern int fogenabled;
- extern int timeofday;
- extern int raindensity;
-
- void gameinit(void)
- {
- int i, x, y;
-
- srand(time(0));
-
- player[0].control.forward=DIK_UP;
- player[0].control.backward=DIK_DOWN;
- player[0].control.left=DIK_LEFT;
- player[0].control.right=DIK_RIGHT;
- player[0].control.leftchange=DIK_PERIOD;
- player[0].control.rightchange=DIK_SLASH;
- player[0].control.fire=DIK_RSHIFT;
-
- player[1].control.forward=DIK_R;
- player[1].control.backward=DIK_F;
- player[1].control.left=DIK_D;
- player[1].control.right=DIK_G;
- player[1].control.leftchange=DIK_LSHIFT;
- player[1].control.rightchange=DIK_Z;
- player[1].control.fire=DIK_X;
-
- player[0].control.rightf=0;
- player[0].control.leftf=0;
- player[1].control.rightf=0;
- player[1].control.leftf=0;
-
- player[0].mode=BRICK_MODE;
- player[1].mode=BRICK_MODE;
-
- player[0].startpos.x=5;
- player[0].startpos.y=10;
- player[1].startpos.x=25;
- player[1].startpos.y=10;
-
- player[0].pos.x=(float)(player[0].startpos.x*10+5);
- player[0].pos.y=(float)(player[0].startpos.y*10+5);
- player[0].rot=0;
-
- player[1].pos.x=(float)(player[1].startpos.x*10+5);
- player[1].pos.y=(float)(player[1].startpos.y*10+5);
- player[1].rot=180;
-
- player[0].lives=5;
- player[1].lives=5;
-
- player[0].ammo.bricks=15;
- player[0].ammo.mines=15;
- player[0].ammo.missles=10;
- player[0].ammo.grenades=5;
- player[1].ammo.bricks=10;
- player[1].ammo.mines=10;
- player[1].ammo.missles=10;
- player[1].ammo.grenades=5;
-
- player[0].missleload=0;
- player[1].missleload=0;
-
- winner=2;
-
- for(y=0; y<21; y++)
- {
- for(x=0; x<31; x++)
- {
- heightmap[x][y]=rand()%6;
- grid[x][y]=0;
- }
- }
-
- for(i=0; i<50; i++)
- {
- x=rand()%30;
- y=rand()%20;
- if(grid[x][y]==EMPTY)
- grid[x][y]=MINE;
- }
- for(i=0; i<10; i++)
- {
- x=rand()%30;
- y=rand()%20;
- if(grid[x][y]==EMPTY)
- grid[x][y]=BRICK1;
- }
- for(i=0; i<10; i++)
- {
- x=rand()%30;
- y=rand()%20;
- if(grid[x][y]==EMPTY)
- grid[x][y]=BRICK0;
- }
- for(i=0; i<50; i++)
- {
- x=rand()%30;
- y=rand()%20;
- if(grid[x][y]==EMPTY)
- grid[x][y]=TREE;
- }
- for(i=0; i<50; i++)
- {
- x=rand()%30;
- y=rand()%20;
- if(grid[x][y]==EMPTY)
- grid[x][y]=TRUNK;
- }
-
- grid[player[0].startpos.x][player[0].startpos.y]=REDFLAG;
- grid[player[1].startpos.x][player[1].startpos.y]=BLUEFLAG;
-
- grid[player[0].startpos.x+3][player[0].startpos.y]=REDGATE;
- grid[player[1].startpos.x-3][player[1].startpos.y]=BLUEGATE;
-
- nextexplosionslot=0;
- for(i=0; i<50; i++)
- explosion[i].stage=0;
-
- nextmissleslot=0;
- for(i=0; i<20; i++)
- missle[i].active=0;
-
- nextsmokeslot=0;
- for(i=0; i<50; i++)
- smoke[i].active=0;
-
- nextgrenadeslot=0;
- for(i=0; i<20; i++)
- grenade[i].stage=0;
-
- nextparticleslot=0;
- for(i=0; i<500; i++)
- particle[i].life=0;
-
- nextdebrisslot=0;
- for(i=0; i<100; i++)
- debris[i].active=0;
-
- for(i=0; i<500; i++)
- {
- rain[i].pos.x=(float)(rand()%300);
- rain[i].pos.y=(float)(rand()%200);
- rain[i].pos.z=(float)(rand()%100);
- }
-
- poweruptime=125;
-
- wall=300;
-
- cameraangle=20;
-
- fogenabled=0;
- timeofday=1;
- raindensity=0;
-
- ifstream fin("data/explosionmap.twl");
- for(y=0; y<20; y++)
- {
- for(x=0; x<20; x++)
- {
- fin >> explosionmap[x][y];
- }
- }
-
- calclightmap();
- }
-
- // All Setup For OpenGL Goes Here:
- int InitGL(GLvoid)
- {
- if (!DI_Init()) // Initialize DirectInput
- return 0;
- if (!LoadGLTextures()) // Jump To Texture Loading Routine
- return FALSE; // If Texture Didn't Load Return FALSE
- BuildFont(); // Build The Font
- BuildLists(); // Jump To The Code That Creates Our Display Lists
- glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Select The Type Of Blending
- glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
- glShadeModel(GL_SMOOTH); // Enable Smooth Shading
- glCullFace(GL_BACK);
- glDisable(GL_CULL_FACE);
- glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
- glClearDepth(1.0f); // Depth Buffer Setup
- glEnable(GL_DEPTH_TEST); // Enables Depth Testing
- glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
-
- return TRUE; // Initialization Went OK
- }
-
-
- // Load Bitmaps And Convert To Textures:
- int LoadGLTextures(void)
- {
- int Status=FALSE, i; // Status Indicator
- AUX_RGBImageRec *TextureImage[TOTAL_TEXTURES]; // Create Storage Space For The Textures
- memset(TextureImage,0,sizeof(void *)*TOTAL_TEXTURES); // Set The Pointer To NULL
- if ((TextureImage[FONT_TEXTURE]=LoadBMP("Data/Font.bmp")) &&
- (TextureImage[GROUND_TEXTURE]=LoadBMP("Data/Ground.bmp")) &&
- (TextureImage[MINE_TEXTURE]=LoadBMP("Data/Mine.bmp")) &&
- (TextureImage[BRICK1_TEXTURE]=LoadBMP("Data/Brick1.bmp")) &&
- (TextureImage[BRICK0_TEXTURE]=LoadBMP("Data/Brick0.bmp")) &&
- (TextureImage[TRUNK_TEXTURE]=LoadBMP("Data/Trunk.bmp")) &&
- (TextureImage[TREE_TEXTURE]=LoadBMP("Data/Tree.bmp")) &&
- (TextureImage[REDTANK_TEXTURE]=LoadBMP("Data/Tankred.bmp")) &&
- (TextureImage[BLUETANK_TEXTURE]=LoadBMP("Data/Tankblue.bmp")) &&
- (TextureImage[DEBRIS_TEXTURE]=LoadBMP("Data/Debris.bmp")) &&
- (TextureImage[WALL_TEXTURE]=LoadBMP("Data/Wall.bmp")) &&
- (TextureImage[MISSLE_TEXTURE]=LoadBMP("Data/Missle.bmp")) &&
- (TextureImage[SMOKE_TEXTURE]=LoadBMP("Data/Smoke.bmp")) &&
- (TextureImage[LIFE_POWERUP_TEXTURE]=LoadBMP("Data/Lifepu.bmp")) &&
- (TextureImage[BRICK_POWERUP_TEXTURE]=LoadBMP("Data/Brickpu.bmp")) &&
- (TextureImage[MINE_POWERUP_TEXTURE]=LoadBMP("Data/Minepu.bmp")) &&
- (TextureImage[MISSLE_POWERUP_TEXTURE]=LoadBMP("Data/Misslepu.bmp")) &&
- (TextureImage[GRENADE_POWERUP_TEXTURE]=LoadBMP("Data/Grenadpu.bmp")))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(TOTAL_TEXTURES, &texture[0]); // Create The Texture
- for (i=0; i<TOTAL_TEXTURES; i++)
- {
- glBindTexture(GL_TEXTURE_2D, texture[i]);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
- }
- for (i=0; i<TOTAL_TEXTURES; i++)
- {
- if (TextureImage[i]) // If Texture Exists
- {
- if (TextureImage[i]->data) // If Texture Image Exists
- {
- free(TextureImage[i]->data); // Free The Texture Image Memory
- }
- free(TextureImage[i]); // Free The Image Structure
- }
- }
- }
- return Status; // Return The Status
- }
-
- void calclightmap(void)
- {
- float Ax, Ay, Az, Bx, By, Bz, normalx, normaly, normalz, l;
- int x, y, L1, L2;
-
- for(y=0; y<20; y++)
- {
- for(x=0; x<30; x++)
- {
- Ax=(float)((x*10-150)-(x*10-140));
- Ay=(float)((y*10-100)-(y*10-100));
- Az=(float)(heightmap[x][y]-heightmap[x+1][y]);
- Bx=(float)((x*10-150)-(x*10-140));
- By=(float)((y*10-90)-(y*10-100));
- Bz=(float)(heightmap[x][y+1]-heightmap[x+1][y]);
-
- normalx=(Ay*Bz)-(By*Az);
- normaly=(Az*Bx)-(Bz*Ax);
- normalz=(Ax*By)-(Bx*Ay);
-
- l=(float)sqrt(normalx*normalx + normaly*normaly + normalz*normalz);
- normalx=normalx/l;
- normaly=normaly/l;
- normalz=normalz/l;
-
- L1=(int)((float)normalz*(float)255);
-
- Ax=(float)((x*10-140)-(x*10-140));
- Ay=(float)((y*10-100)-(y*10-90));
- Az=(float)(heightmap[x+1][y]-heightmap[x+1][y+1]);
- Bx=(float)((x*10-150)-(x*10-140));
- By=(float)((y*10-90)-(y*10-90));
- Bz=(float)(heightmap[x][y+1]-heightmap[x+1][y+1]);
-
- normalx=(Ay*Bz)-(By*Az);
- normaly=(Az*Bx)-(Bz*Ax);
- normalz=(Ax*By)-(Bx*Ay);
-
- l=(float)sqrt(normalx*normalx + normaly*normaly + normalz*normalz);
- normalx=normalx/l;
- normaly=normaly/l;
- normalz=normalz/l;
-
- L2=(int)((float)normalz*(float)255);
-
- lightmap[x][y]=(L1+L2)/2;
-
- if(lightmap[x][y]<70)
- lightmap[x][y]+=180;
- lightmap[x][y]=(unsigned char)(lightmap[x][y]*.4);
- }
- }
- }
-